package com.miao.teacher.models;

import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.media.MediaPlayer;

public class RecordItem  extends MediaPlayer implements
Comparable<RecordItem> {
	public File file;
	public int starttime;
	public int seq;
	public boolean isReady;
	public String tvDes;
	
	public RecordItem(File _file) {
		super();
		isReady = false;
		file = _file;
		try {
			setDataSource(file.toString());
			prepare();
			isReady = true;
		} catch (IllegalArgumentException e) {
			isReady = false;
		} catch (SecurityException e) {
			isReady = false;
		} catch (IllegalStateException e) {
			isReady = false;
		} catch (IOException e) {
			isReady = false;
		}
		Pattern p = Pattern.compile("[0-9]+_rev.amr");
		Matcher m = p.matcher(file.toString());
		if (m.find()) {
			String starttimeString = m.group().replace("_rev.amr", "");
			starttime = Integer.valueOf(starttimeString);
			seq = -1;
			//Log.i(TAG + ".RecordItem", "starttime = " + starttime);
		}
		p = Pattern.compile("[0-9]+_res.amr");
		m = p.matcher(file.toString());
		if (m.find()) {
			String seqString = m.group().replace("_res.amr", "");
			seq = Integer.valueOf(seqString);
			starttime = -1;
			//Log.i(TAG + ".RecordItem", "seq = " + seq);
		}
	}

	public boolean isReady() {
		return isReady;
	}

	public void remove() {
		if (file.exists()) {
			file.delete();
		}
	}

	@Override
	public int compareTo(RecordItem another) {
		if (starttime > another.starttime) {
			return 1;
		} else if (starttime == another.starttime) {
			if (seq > another.seq)
				return 1;
			else
				return -1;
		} else {
			return -1;
		}
	}

}
